package com.atguigu.gmall.item.controller;


import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.pool.RunTreadPool;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductClientController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("/api/item")
public class ItemProduct {

    @Autowired
    ProductClientController productClientController;

    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @GetMapping("/{skuId}")
    public Map<String,Object> getItem(@PathVariable(name = "skuId") Long skuId){

        Map<String, Object> stringObjectMap = new HashMap<>();
        CompletableFuture<SkuInfo> completableFutureSkuInfo = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productClientController.getSkuInfo(skuId);
            stringObjectMap.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> categoryView1 = completableFutureSkuInfo.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productClientController.getCategoryView(skuInfo.getCategory3Id());
            stringObjectMap.put("categoryView", categoryView);
        }, threadPoolExecutor);

        CompletableFuture<Void> price1 = CompletableFuture.runAsync(() -> {
            BigDecimal price = productClientController.getPrice(skuId);
            stringObjectMap.put("price", price);
        }, threadPoolExecutor);

        CompletableFuture<Void> spuSaleAttrList = CompletableFuture.runAsync(() -> {
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productClientController.getSpuSaleAttrListCheckBySku(skuId);
            stringObjectMap.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        }, threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJson = completableFutureSkuInfo.thenAcceptAsync(skuInfo -> {
            Map<String, String> skuValueIdsMap = productClientController.getSkuValueIdsMap(skuInfo.getSpuId());
            stringObjectMap.put("valuesSkuJson", JSON.toJSONString(skuValueIdsMap));
        }, threadPoolExecutor);


        CompletableFuture.allOf(completableFutureSkuInfo,categoryView1,price1,spuSaleAttrList,valuesSkuJson).join();

        /*SkuInfo skuInfo = productClientController.getSkuInfo(skuId);
        stringObjectMap.put("skuInfo",skuInfo);

        BaseCategoryView categoryView = productClientController.getCategoryView(skuInfo.getCategory3Id());
        stringObjectMap.put("categoryView",categoryView);

        BigDecimal price = productClientController.getPrice(skuId);
        stringObjectMap.put("price",price);

        List<SpuSaleAttr> spuSaleAttrListCheckBySku = productClientController.getSpuSaleAttrListCheckBySku(skuId);
        stringObjectMap.put("spuSaleAttrList",spuSaleAttrListCheckBySku);

        Map<String, String> skuValueIdsMap = productClientController.getSkuValueIdsMap(skuInfo.getSpuId());
        stringObjectMap.put("valuesSkuJson", JSON.toJSONString(skuValueIdsMap));
        System.out.println( JSON.toJSONString(skuValueIdsMap));*/


        return stringObjectMap;
    }


}
